<template>
  <div class="app-container">
    <el-dialog title="选择产品" :visible.sync="open" width="1400px" v-dialogDrag append-to-body @close='close'>
      <!-- 搜索工作栏 -->
      <el-form :model="queryParams" ref="queryForm" size="small" v-show="showSearch" label-width="68px">
        <el-row>
          <el-col :span="4">
            <el-form-item label="产品信息" prop="itemId">
              <el-input v-model="queryParams.itemCode" placeholder="请选择产品信息" disabled >
                <el-button slot="append" icon="el-icon-search" @click="openSelectItem = true"></el-button>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="工厂" prop="factoryId">
              <selectFactory v-model="queryParams.factoryId"></selectFactory>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="生产车间" prop="workshopId">
              <selectWorkshop v-model="queryParams.workshopId"></selectWorkshop>

            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="部门" prop="deptId">
              <selectDept v-model="queryParams.deptId"></selectDept>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <!-- 列表 -->
      <el-table v-loading="loading" :data="list" @row-dblclick='clickRow'>
        <el-table-column label="序号" align="center" prop="id" />
        <el-table-column label="工厂" align="center" prop="factoryName" />
        <el-table-column label="生产车间" align="center" prop="workshopName" />
        <el-table-column label="部门" align="center" prop="deptName" />
        <el-table-column label="产品名称" align="center" prop="itemName" />
        <el-table-column label="产品代码" align="center" prop="itemCode" />
        <el-table-column label="产品规格" align="center" prop="spec" />
        <el-table-column label="产品图号" align="center" prop="drawCode" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template v-slot="scope">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="selectItemProduce(scope.row)" >选择</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页组件 -->
      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize" @pagination="getList"/>
    </el-dialog>
    <!-- 选择产品组件 -->
    <selectItem :openSelectItem.sync='openSelectItem' @selectItem='selectItem'></selectItem>
  </div>
</template>

<script>
import { getItemProducePage } from "@/api/mes/itemProduce";
import SelectItem from "@/components/SelectItem"
import SelectFactory from "@/components/SelectFactory"
import SelectWorkshop from '@/components/SelectWorkshop/index.vue'
import SelectDept from "@/components/SelectDept"
export default {
  name: "SelectItemProduce",
  components: {
    SelectFactory,
    SelectWorkshop,
    SelectDept,
    SelectItem,

  },
  props: {
    openSelectItemProduce: {
      required: true,
      default: false
    }
  },
  watch: {
    openSelectItemProduce(newVal) {
      this.open = newVal;
    }
  },
  data() {
    return {
      open: this.openSelectItemProduce,
      // 遮罩层
      loading: true,
      // 打开选择产品组件
      openSelectItem:false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品通用信息列表
      list: [],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        factoryId: null,
        deptId: null,
        workshopId: null,
        itemId: null,
        itemCode: null,
      },
    }
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 执行查询
      getItemProducePage(this.queryParams).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 行点击 */
    clickRow(row, column, event){
      this.selectItemProduce(row);
    },
    /** 选择产品 */
    selectItemProduce(row) {
      this.$emit('selectItemProduce', row);
      this.close()
    },
    close() {
      this.open = false;
      this.$emit('update:openSelectItemProduce', this.open)
    },
    /** 产品选择回调事件 */
    selectItem(item){
      this.queryParams.itemId = item.id;
      this.queryParams.itemCode = item.code;
    },
  }
};
</script>

